package cc.lucien.demo01;

public class LinearSearch {
    public LinearSearch(){}

    public static <E> int search(E[] arr, E target){
        for (int i = 0; i < arr.length; i++)
            if (arr[i].equals(target))
                return i;
        return -1;
    }

    public static void main(String[] args) {
        //测试规模
        int[] dataSize = {1000000,10000000};
        for (int n : dataSize){
            Integer[] data = ArrayGenerator.generateOrderedArray(n);

            long startTime = System.nanoTime();
            for (int k = 0; k < 100; k++)
                LinearSearch.search(data,n);
            long endTime = System.nanoTime();
            double time = (endTime - startTime) / 1000000000.0;
            System.out.println("n = " + n + ", 100 runs :" + time + "s");
        }
    }
}
